home *** CD-ROM | disk | FTP | other *** search
- /*
- Harvest C
- Copyright 1992 Eric W. Sink. All rights reserved.
-
- This file is part of Harvest C.
-
- Harvest C is free software; you can redistribute it and/or modify
- it under the terms of the GNU Generic Public License as published by
- the Free Software Foundation; either version 2, or (at your option)
- any later version.
-
- Harvest C is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with Harvest C; see the file COPYING. If not, write to
- the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- Harvest C is not in any way a product of the Free Software Foundation.
- Harvest C is not GNU software.
- Harvest C is not public domain.
-
- This file may have other copyrights which are applicable as well.
-
- */
-
- /*
- DMacroCollector.c
- */
-
- #include "DMacroCollector.h"
- #include "CList.h"
- #include "DTokenStream.h"
-
- void DMacroCollector::IMacroCollector(DMacro *aMacro)
- {
- int i;
- theMacro = aMacro;
- startingParenLevel = -1; /* DTokenStream will set this */
- currentArg = -1;
- theArgs = (DTokenStream **) icemalloc(theMacro->theParams->GetNumItems() * sizeof(DTokenStream *));
- for (i=0;i<theMacro->theParams->GetNumItems();i++)
- {
- theArgs[i] = new DTokenStream;
- theArgs[i]->ITokenStream(NULL);
- }
- }
-
- void DMacroCollector::Expand(DTokenStream *into,CSymbolList *beingExpanded)
- {
- /* Copy the tokens in theMacro->expansion into into. Add theMacro
- to beingExpanded. For each token in theMacro->expansion, check to
- see if it is in theMacro->theParams. If not, into->Add(it). If so,
- loop through all tokens in the corresponding entry in theArgs[]. */
- int i;
- int j;
- long countTokens;
- long countParamTokens;
- DToken *aToken;
- DSymbol *aParam;
- int argIndex;
- beingExpanded->Append((void *) theMacro);
- countTokens = theMacro->expansion->GetNumItems();
- for (i=1;i<=countTokens;i++) {
- aToken = theMacro->expansion->GetNthToken(i);
- /* Check to see if it is in theMacro->theParams */
- if (aParam = theMacro->theParams->Find(aToken->name)) {
- beingExpanded->Remove((void *) theMacro);
- /* Look up the param in theArgs */
- argIndex = theMacro->theParams->FindIndex((void *) aParam);
- /* Now insert all tokens in theArgs[argIndex] */
- countParamTokens = theArgs[argIndex]->GetNumItems();
- for (j=1;j<=countParamTokens;j++) {
- aToken = theArgs[argIndex]->GetNthToken(j);
- into->Add(aToken,beingExpanded);
- }
- beingExpanded->Append((void *) theMacro);
- }
- else {
- into->Add(aToken,beingExpanded);
- }
- }
- beingExpanded->Remove((void *) theMacro);
- }
-
- void DMacroCollector::StartNextArg()
- {
- currentArg++;
-
- }
-
- void DMacroCollector::AddToArg(DToken *aToken)
- {
- theArgs[currentArg]->Add(aToken,NULL);
-
- }
-
- void DMacroCollector::Dispose(void)
- {
- int i;
- for (i=0;i<theMacro->theParams->GetNumItems();i++)
- {
- theArgs[i]->Dispose();
- }
- icefree(theArgs);
- inherited::Dispose();
- }
-